<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Install_controller extends CI_Controller {
	/**
		*	index: 
		*
		*	@author:	Philipp Panka	<ppanka@gedak.de>
		*/
	public function index()
	{
		$this->welcome();
	}
	
	/**
		*	welcome: welcome page
		*
		*	@author:	Philipp Panka	<ppanka@gedak.de>
		*/
	public function welcome(){
		$data = array();
		$data['active_page'] = 0;
		$this->template->write_view('menu', 'menu_view', $data);
		$this->template->write_view('content', 'welcome_message');
		$this->template->render();
	}
	
	/**
		*	database: database page
		*
		*	@author:	Philipp Panka	<ppanka@gedak.de>
		*/
	public function database(){
		$data = array();
		$data['active_page'] = 1;
		$db_data = $this->session->userdata('database');
		if($db_data){
			$data['server'] = $db_data['server'];
			$data['username'] = $db_data['username'];
			$data['password'] = $db_data['password'];
			$data['database'] = $db_data['database'];
		}else{
			$data['server'] = "";
			$data['username'] = "";
			$data['password'] = "";
			$data['database'] = "";
		}
		
		$this->template->write_view('menu', 'menu_view', $data);
		$this->template->write_view('content', 'database_view', $data);
		$this->template->render();
	}
	
	/**
		*	set_database: set database data
		*
		*	@author:	Philipp Panka	<ppanka@gedak.de>
		*/
	public function set_database(){
		$this->load->model('install_model');
		$this->form_validation->set_rules('server', 'Server', 'required');
		$this->form_validation->set_rules('username', 'Benutzername', 'required');
		$this->form_validation->set_rules('password', 'Passwort', 'required');
		$this->form_validation->set_rules('database', 'Datenbank', 'required');
		if ($this->form_validation->run() == FALSE){
			$validation = false;
		}else{
			if(!$this->install_model->check_db($this->input->post('server'), $this->input->post('username'), $this->input->post('password'), $this->input->post('database'))){
				setMessage($this, 'error', 'Verbindung zur Datenbank konnte nicht hergestellt werden!');
				$validation = false;
			}else{
				$validation = true;
			}
		}
		
		if (!$validation){
			if(validation_errors()){
				setMessage($this, 'error', validation_errors());
			}
			$this->database();
		}else{
			$this->install_model->setDatabase($this->input->post('server'), $this->input->post('username'), $this->input->post('password'), $this->input->post('database'));
			header('Location: ' . base_url() . 'index.php/installation/administrator');
		}
	}
	
	/**
		*	administrator: administrator page
		*
		*	@author:	Philipp Panka	<ppanka@gedak.de>
		*/
	public function administrator(){
		if(!$this->session->userdata('database')){
			header('Location: ' . base_url() . 'index.php/installation/database');
			exit;
		}
		$data = array();
		$admin_data = $this->session->userdata('administrator');
		if($admin_data){
			$data['admin_password'] = $admin_data['admin_password'];
			$data['email'] = $admin_data['email'];
		}else{
			$data['admin_password'] = "";
			$data['email'] = "";
		}
		$data['active_page'] = 2;
		$this->template->write_view('menu', 'menu_view', $data);
		$this->template->write_view('content', 'administrator_view', $data);
		$this->template->render();
	}
	
	/**
		*	set_administrator: set administrator data
		*
		*	@author:	Philipp Panka	<ppanka@gedak.de>
		*/
	public function set_administrator(){
		$this->load->model('install_model');
		$this->form_validation->set_rules('admin_password', 'Passwort', 'required');
		$this->form_validation->set_rules('email', 'E-Mail', 'required|valid_email');
		
		if ($this->form_validation->run() == FALSE){
			setMessage($this, 'error', validation_errors());
			$this->administrator();
		}else{
			$this->install_model->setAdministrator($this->input->post('admin_password'), $this->input->post('email'));
			header('Location: ' . base_url() . 'index.php/installation/school');
		}
	}
	
	/**
		*	school: school page
		*
		*	@author:	Philipp Panka	<ppanka@gedak.de>
		*/
	public function school(){
		
		if(!$this->session->userdata('administrator')){
			header('Location: ' . base_url() . 'index.php/installation/administrator');
			exit;
		}
		
		$data = array();
		$admin_data = $this->session->userdata('school');
		if($admin_data){
			$data['schoolname'] = $admin_data['schoolname'];
			$data['schoolmail'] = $admin_data['schoolmail'];
			$data['kbomail'] = $admin_data['kbomail'];
			$data['sender'] = $admin_data['sender'];
		}else{
			$data['schoolname'] = "";
			$data['schoolmail'] = "";
			$data['kbomail'] = "";
			$data['sender'] = "";
		}
		
		$data['active_page'] = 3;
		$this->template->write_view('menu', 'menu_view', $data);
		$this->template->write_view('content', 'school_view', $data);
		$this->template->render();
	}
	
	/**
		*	set_school: set school data
		*
		*	@author:	Philipp Panka	<ppanka@gedak.de>
		*/
	public function set_school(){
		$this->load->model('install_model');
		$this->form_validation->set_rules('schoolname', 'Schulname', 'required');
		$this->form_validation->set_rules('schoolmail', 'E-Mail(Schule)', 'required|valid_email');
		$this->form_validation->set_rules('kbomail', 'E-Mail(KBO)', 'required|valid_email');
		$this->form_validation->set_rules('sender', 'Absender', 'required');
		
		if ($this->form_validation->run() == FALSE){
			setMessage($this, 'error', validation_errors());
			$this->school();
		}else{
			$this->install_model->setSchool($this->input->post('schoolname'), $this->input->post('schoolmail'), $this->input->post('kbomail'), $this->input->post('sender'));
			header('Location: ' . base_url() . 'index.php/installation/finish');
		}
	}
	
	/**
		*	finish: finish page
		*
		*	@author:	Philipp Panka	<ppanka@gedak.de>
		*/
	public function finish(){
		if(!$this->session->userdata('database') OR !$this->session->userdata('administrator') OR !$this->session->userdata('school')){
			header('Location: ' . base_url() . 'index.php/installation/database');
			exit;
		}
		$data = array();
		$data['active_page'] = 4;
		$this->template->write_view('menu', 'menu_view', $data);
		$this->template->write_view('content', 'finish_view', $data);
		$this->template->render();
	}
}